package cool.leeson.library.entity.library.simple;

import org.springframework.beans.factory.annotation.Value;

import java.io.*;
import java.nio.file.*;

public class RulesSimple {

    // 存储规则文件的路径
    private static final String RULES_FILE_PATH = "src/main/resources/config/rules.txt";

    // 读取规则
    public static String getRules() {
        StringBuilder rules = new StringBuilder();
        try {
            // 读取规则文件
            Path filePath = Paths.get(RULES_FILE_PATH);
            if (Files.notExists(filePath)) {
                return "规则文件未找到";
            }

            // 读取文件内容
            rules.append(new String(Files.readAllBytes(filePath), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
            return "读取规则失败";
        }
        return rules.toString();
    }

    // 设置规则
    public static void setRules(String newRules) {
        // 将规则内容直接写入文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(RULES_FILE_PATH))) {
            writer.write(newRules);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

